﻿namespace Leetcode.N0011;

public class Solution
{
    public int MaxArea(int[] height)
    {

        var from = 0;
        var to = height.Length - 1;
        var fromHeight = height[from];
        var toHeight = height[to];
        var maxArea = Math.Min(height[from], height[to]) * (to - from);

        while (from < to - 1)
        {
            if (height[from] <= height[to])
            {
                from++;
                if (height[from] <= fromHeight)
                {
                    continue;
                }
                else
                {
                    fromHeight = height[from];
                }
            }
            else
            {
                to--;
                if (height[to] <= toHeight)
                {
                    continue;
                }
                else
                {
                    toHeight = height[to];
                }
            }

            maxArea = Math.Max(maxArea, Math.Min(height[from], height[to]) * (to - from));
        }
        return maxArea;
    }
}
